package com.example.clipboardsyncapp.data.store

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "user_preferences")

class UserPreferences(private val context: Context) {
    
    companion object {
        private val AUTH_COOKIE = stringPreferencesKey("auth_cookie")
    }

    val authCookie: Flow<String?> = context.dataStore.data
        .map { preferences ->
            preferences[AUTH_COOKIE]
        }

    suspend fun saveAuthCookie(cookie: String) {
        context.dataStore.edit { preferences ->
            preferences[AUTH_COOKIE] = cookie
        }
    }

    suspend fun clearAuthCookie() {
        context.dataStore.edit { preferences ->
            preferences.remove(AUTH_COOKIE)
        }
    }
} 